package com.udi.project.business.local.di.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.udi.common.utils.security.ShiroUtils;
import com.udi.framework.aspectj.lang.enums.BusinessType;
import com.udi.framework.web.controller.BaseController;
import com.udi.framework.web.domain.AjaxResult;
import com.udi.project.business.base.domain.BaseCorpVo;
import com.udi.project.business.local.di.domain.*;
import com.udi.project.business.local.di.service.DiGenerateService;
import com.udi.project.business.local.di.thread.Submit;
import com.udi.project.business.pojo.damain.dtPkg;
import com.udi.project.business.utils.httpApi.OkHttp1;

import com.udi.project.system.user.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import com.udi.project.business.utils.redis.RedisUtils;
import com.udi.framework.aspectj.lang.annotation.Log;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Admin
 * Di生成控制器
 */
@Controller
@RequestMapping("/digener")
@Slf4j
public class DiGenerateController extends BaseController {

    @Autowired
    private DiGenerateService diGenerateService;

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private OkHttp1 okHttp1;

    /**
    *
     * @Description:DI生成控制器
     * @param packageCode 包装类别
     * @param deptName 部门id
     * @param frequency 码生成数量
     * @return: java.lang.String
     * @Date: 2021/8/10 14:18
     *
     *http://localhost:8080/digener/getdi?packageCode=10&deptName=123&internationality=690&frequency=1
     */
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult getDi(DtDi DtDi){
        try
        {
            Integer corpCode = Integer.parseInt(DtDi.getCorpCode());
            String deptId = DtDi.getDeptId();
            Integer type = Integer.parseInt(DtDi.getType());
            Integer num = Integer.parseInt(DtDi.getNum());
            return toAjax(diGenerateService.diGenerate(corpCode,type,deptId,num));
        }catch(Exception e){
            e.printStackTrace();
            return error(e.getMessage());
        }
    }


    /**
     *
     * @param
     * @return java.lang.String
     * @author yuxin
     * @creed: Talk is cheap,show me the code
     * @date 2021/8/23 11:12
     */
    @GetMapping("/getToken")
    @ResponseBody
    public String getToken() throws IOException {
       redisUtils.set("to", okHttp1.GetCredentials());
       return "ss";
    }

    /**
     * http://localhost:8080/digener/getTo
     *
     * */
    @GetMapping("/getTo")
    public void testController(){
        Submit submit = new Submit();
        submit.start();
    }

    /**
     * @Description: 获取医疗器械唯一标识数据
     * @param requestType 产品标识记录新增开始时间 时间格式： yyyy-MM-dd HH:mm:ss
     * @param rangeValue 产 品 标 识 记 录 新 增 结 束 时 间
     * @param currentPageNumber 请求分页数,初始请求时，从 1 开始
     * @return: com.alibaba.fastjson.JSONObject
     * @Date: 2021/8/13 8:59
     * http://localhost:8080/digener/instrument?requestType=2021-08-15 00:00:00&rangeValue=2021-08-16 00:00:00&currentPageNumber=1
     * 
     */
    @GetMapping("/instrument")
    @ResponseBody
    public JSONObject getInstrument(String requestType,String rangeValue,String currentPageNumber){
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = okHttp1.GetLogo(requestType,rangeValue,currentPageNumber);
        }catch (SocketTimeoutException e) {
            e.printStackTrace();
            return JSONObject.parseObject("{\"ims\":\"访问超时\"}");
        }catch (IOException e){
            e.printStackTrace();
        }
       return jsonObject;
    }



    /**
     *
     * @Description: 获取历史版本数据
     * @param keyOne 标识数据记录 key1
     * @param PageNumber 请求分页数,初始请求时，从 1 开始
     * @return: com.alibaba.fastjson.JSONObject 返回具体的历史数据
     * @Date: 2021/8/3 9:14
     * http://localhost:8080/digener/getHistory?keyOne=10241928&PageNumber=1
     */
    @RequestMapping("/getHistory")
    @ResponseBody
    public JSONObject getHistoryData(String keyOne,String PageNumber){
        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject = okHttp1.History(keyOne,PageNumber);
        }catch (SocketTimeoutException e){
            e.printStackTrace();
            return JSONObject.parseObject("{\"ims\":\"访问超时\"}");
        }catch (IOException e){
            e.printStackTrace();
        }
        System.out.println("返回数据--------->"+jsonObject);
        return jsonObject;
    }

}
